package cn.lyh.qinfeng.plugin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.lyh.qinfeng.pojo.Memberinfo;
import cn.lyh.qinfeng.service.BaoMingService;
import net.lz1998.cq.event.message.CQGroupMessageEvent;
import net.lz1998.cq.robot.CQPlugin;
import net.lz1998.cq.robot.CoolQ;
import net.lz1998.cq.utils.CQCode;

/**
 * @author lyh
 * @version 1.0
 * @date 2020/3/19
 * @Description
 */
@Component
public class BaoMingPlugin extends CQPlugin {
    //2T四个奶

    @Autowired
    BaoMingService baoMingService;

    @Override
    public int onGroupMessage(CoolQ cq, CQGroupMessageEvent event) {
        //我要报名 昵称 职业 内外功T奶 1
        // 优化报名系统 格式改为 我要报名 职业 职业分类 编号 昵称通过发送人获取
        // 取消报名格式为 取消报名 1
        String baomingMsg = event.getMessage().trim();
        System.out.println("baomingMsg = " + baomingMsg);
        Integer userId = Math.toIntExact(event.getUserId());
        String resultMsg = "";
        long groupId = event.getGroupId();
        if ("我要报名".equals(baomingMsg.split(" ")[0])) {
            //假如是我要报名则可继续拆分然后去数据库核验是否存在这个职业
            String nickName = event.getSender().getNickname();
            String mebOccupation = baomingMsg.trim().split(" ")[1];
            String mebClass = baomingMsg.trim().split(" ")[2];
            Integer tuanId = Integer.valueOf(baomingMsg.trim().split(" ")[3]);
            //通过职业分类去查询数据库检查是否存在 2T4奶1双休
            int checkMebRst = baoMingService.checkMeb(mebClass);
            System.out.println("checkMebRst = " + checkMebRst);
            // 报名之间先去查询团员信息是否满25个包括老板
            List<Memberinfo> allInfo = baoMingService.getAllInfo(tuanId);
            System.out.println(allInfo.size());
            if (allInfo.size() < 25) {
                //如果职业是T且数据库中的T数据小于等于两个时候则可以报名
                if ("T".equals(mebClass) && checkMebRst < 2) {
                    int i = baoMingService.insertMeb(userId, nickName, mebOccupation, mebClass, tuanId);
                    if (i > 0) {
                        resultMsg += "报名成功!";
                    } else {
                        resultMsg += "坑满！";
                    }
                } else if ("奶妈".equals(mebClass) && checkMebRst < 4) {
                    int i = baoMingService.insertMeb(userId, nickName, mebOccupation, mebClass, tuanId);
                    if (i > 0) {
                        resultMsg += "报名成功！";
                    } else {
                        resultMsg += "坑满！";
                    }
                } else if ("双休奶".equals(mebClass) && checkMebRst < 1) {
                    int i = baoMingService.insertMeb(userId, nickName, mebOccupation, mebClass, tuanId);
                    if (i > 0) {
                        resultMsg += "报名成功！";
                    } else {
                        resultMsg += "坑满！";
                    }
                } else if ("外功".equals(mebClass) && checkMebRst < 8) {
                    int i = baoMingService.insertMeb(userId, nickName, mebOccupation, mebClass, tuanId);
                    if (i > 0) {
                        resultMsg += "报名成功！";
                    } else {
                        resultMsg += "坑满！";
                    }
                } else if ("内功".equals(mebClass) && checkMebRst < 8) {
                    int i = baoMingService.insertMeb(userId, nickName, mebOccupation, mebClass, tuanId);
                    if (i > 0) {
                        resultMsg += "报名成功！";
                    } else {
                        resultMsg += "坑满！";
                    }
                }
                else{
                    resultMsg += "坑满！";
                }
            } else {
                resultMsg += "坑满！";
            }
            cq.sendGroupMsg(groupId, resultMsg, false);
            return MESSAGE_BLOCK;
        }
        else if ("取消报名".equals(baomingMsg.split(" ")[0])){
            Integer tuanId = Integer.valueOf(baomingMsg.trim().split(" ")[1]);
            //通过团队编号与自己的QQ号去删除 如果返回值<=0则表示没报名
            int deleteRst = baoMingService.deleteUserInfo(tuanId, userId);
            if (deleteRst > 0) {
                resultMsg = CQCode.at(Long.valueOf(userId)) +"取消报名成功";
            }
            else {
                resultMsg = CQCode.at(Long.valueOf(userId)) + "尚未查询到您已报名！取消失败";
            }
            cq.sendGroupMsg(groupId, resultMsg, false);
            return MESSAGE_BLOCK;
        }
        return MESSAGE_IGNORE;
    }
}
